package com.mainService;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.dubboInterface.merchant.chartHandleInterface.ExcelRead;
import com.dubboInterface.merchant.chartHandleInterface.ExcelWrite;
import com.mainService.mapper.SmokeMapper;
import com.projectCommon.bean.Smoke;
import com.projectCommon.bean.easyExcel.putTactics.AnticipatedOrderData;
import com.projectCommon.bean.easyExcel.putTactics.GearExtendSelection;
import com.projectCommon.bean.easyExcel.putTactics.PriceSelection;
import com.projectCommon.bean.easyExcel.putTactics.WeeklySupplyStrategyData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SpringBootTest
public class demo {
    @Reference
    private ExcelRead excelRead;
    @Reference
    private ExcelWrite excelWrite;
    @Autowired
    private SmokeMapper smokeMapper;

    @Test
    public void test() throws IOException {
        File file = new File("C:\\Users\\wy\\Downloads\\2024.10.28-10.31周货源投放策略(太原).xls");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[(int) file.length()];
        fileInputStream.read(bytes);
        String fileName = file.getName();

        //        2. 解析数据  最终得到一个对象，包含全部的数据
        WeeklySupplyStrategyData weeklySupplyStrategyData = excelRead.readWeeklySupplyStrategyData(bytes, fileName);
//            2.1 处理这个数据，将不投放的卷烟去掉
        revisedData(weeklySupplyStrategyData);
//        3. 将数据根据模板写入  这部分应该也是easyExcel 来处理的事情。但是需要使用继续查询数据库的数据，所以在这儿查好传过去
        List<String> encodeList = new ArrayList<>();
        for (AnticipatedOrderData anticipatedOrderData : weeklySupplyStrategyData.getGearSelection().getList()) {
            encodeList.add(anticipatedOrderData.getProductEncoding());
        }

        for (PriceSelection selection : weeklySupplyStrategyData.getPriceSelection()) {
            for (AnticipatedOrderData anticipatedOrderData : selection.getList()) {
                encodeList.add(anticipatedOrderData.getProductEncoding());
            }
        }
        for (GearExtendSelection gearExtendSelection : weeklySupplyStrategyData.getGearExtendSelectionList()) {
            encodeList.add(gearExtendSelection.getList().get(0).getProductEncoding());
        }
        List<Smoke> allSmokeByProductCode = smokeMapper.getAllSmokeByProductCode(encodeList);
        Map<String, Smoke> dbInfo = allSmokeByProductCode.stream().collect(Collectors.toMap(Smoke::getProductCode, smoke -> smoke));
        byte[] bytes1 = excelWrite.weeklySupplyStrategyWrite(ExcelTypeEnum.XLS, weeklySupplyStrategyData, dbInfo);
        FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\Users\\wy\\Downloads\\2222.xls"));
        fileOutputStream.write(bytes1);
    }

    private void revisedData(WeeklySupplyStrategyData weeklySupplyStrategyData) {
        // 移除 gearPutList 中不需要的元素
        weeklySupplyStrategyData.getGearSelection().getList().removeIf(item -> !item.getReleaseOrNo().equals("是"));

        // 使用迭代器移除 gearExtendSelectionList 中不需要的元素
        Iterator<GearExtendSelection> gearExtendSelectionIterator = weeklySupplyStrategyData.getGearExtendSelectionList().iterator();
        while (gearExtendSelectionIterator.hasNext()) {
            GearExtendSelection gearExtendSelection = gearExtendSelectionIterator.next();
            List<AnticipatedOrderData> list = gearExtendSelection.getList();
            if (list != null) {
                list.removeIf(anticipatedOrderData -> !anticipatedOrderData.getReleaseOrNo().equals("是"));
                // 如果列表为空，则移除 gearExtendSelection
                if (list.isEmpty()) {
                    gearExtendSelectionIterator.remove();
                }
            }
        }
    }
}

